<?php

namespace app\user\service\user;

use app\common\exception\LogicException;
use app\common\service\BaseService;
use app\user\logic\user\NotifyMessage;
use app\user\logic\user\SystemNotify;
use think\facade\Request;
use think\Response;

/**
 * 消息通知
 */
class Notify extends BaseService
{

    // 互动消息
    public function getCommentNotify()
    {
        $status = input('?get.status') ? input('get.status/d') : 2;    // 默认2，全部消息
        $data   = app(NotifyMessage::class)->getCommentNotify($status);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 提问消息
    public function getQuestionNotify()
    {
        $status = input('?get.status') ? input('get.status/d') : 2;    // 默认2，全部消息
        $data   = app(NotifyMessage::class)->getQuestionNotify($status);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 课程提醒
    public function getCourseNotify()
    {
        $status = input('?get.status') ? input('get.status/d') : 2;    // 默认2，全部消息
        $data   = app(SystemNotify::class)->getCourseNotify($status);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 系统消息
    public function getSystemNotify()
    {
        $status = input('?get.status') ? input('get.status/d') : 2;    // 默认2，全部消息
        $data   = app(SystemNotify::class)->getSystemNotify($status);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 一键已读
    public function setRead()
    {
        $operate = Request::post('operate/d');
        $object  = Request::post('object/d');
        $id      = Request::post('id/d');

        // 参数验证
        if (!in_array($operate, [0, 1, 2])) {
            throw new LogicException("参数错误");
        }

        // 设置已读
        if ($operate) {
            if (!in_array($object, [1, 2, 3, 4]) || (!$id && $operate == 1)) {
                throw new LogicException("参数错误");
            }
            $mod = in_array($object, [1, 2]) ? app(SystemNotify::class) : app(NotifyMessage::class);
            if ($operate == 1) {
                $mod->setRead($id);
            } else {
                $mod->setAllRead($object);
            }
        }

        // 未读数
        $system_unread   = app(SystemNotify::class)->unreadCount(1);
        $course_unread   = app(SystemNotify::class)->unreadCount(2);
        $comment_unread  = app(NotifyMessage::class)->unreadCount(3);
        $question_unread = app(NotifyMessage::class)->unreadCount(4);

        $data = [
            'comment_unread'  => $comment_unread,
            'question_unread' => $question_unread,
            'system_unread'   => $system_unread,
            'course_unread'   => $course_unread,
        ];
        return $this->parseToData($data, 1, '设置成功');
    }

    /**
     * 未读消息
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/25
     *
     * @return Response
     */
    public function getUnreadNum()
    {
        $unread = app(\app\user\logic\user\Profile::class)->getUnreadNum(false);
        return $this->parseToData($unread, 1, '获取成功');
    }
}
